﻿
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using Okarito;
using Okarito.Utility;

namespace FogBugzTime
{
    /// <summary>
    /// A simple <see cref="FogBugzClient"/> consumer that displays the time worked by the specified user over the past week.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // load the credentials to use
            Uri serverUri;
            string userName;
            string password;
            GetCredentials(args, out serverUri, out userName, out password);

            Console.WriteLine("Requesting time worked for the last week:");

            FogBugzClient client = new FogBugzClient(serverUri);
            using (client.LogOn(userName, password))
            {
                // request time for the last week
                DateTime dtLastWeek = DateTime.Now.AddDays(-7);
                var outputs = Enumerable.Range(0, 7).Select(d =>
                {
                    // get the intervals for this day of the last week
                    DateTime dtQuery = dtLastWeek.AddDays(d);
                    var taskIntervals = client.GetIntervals(dtQuery);
                    return taskIntervals.ContinueWith(intervals =>
                    {
                        // format the day and date
                        string day = string.Format("{0}, {1}: ", dtQuery.DayOfWeek.ToString().Substring(0, 3), dtQuery.ToShortDateString());

                        // format the hours worked
                        TimeSpan total = intervals.Result.Sum(i => i.Duration);
                        string time = total.Ticks == 0 ? "-:--" : string.Format("{0}:{1:00}", total.Hours, total.Minutes);

                        // return the composite output
                        return day + time;
                    });
                });

                // display each output (in order)
                foreach (Task<string> output in outputs)
                    Console.WriteLine(output.Result);
            }
        }

        // Gets credentials from the command-line arguments, from the registry, or by prompting the user.
        private static void GetCredentials(string[] args, out Uri serverUri, out string userName, out string password)
        {
            if (args.Length == 3)
            {
                serverUri = new Uri(args[0]);
                userName = args[1];
                password = args[2];
            }
            else if (!AuthenticationUtility.GetCredentials(out serverUri, out userName, out password))
            {
                Console.Write("FogBugz Server: ");
                serverUri = new Uri(Console.ReadLine());
                Console.Write("User Name: ");
                userName = Console.ReadLine();
                Console.Write("Password: ");
                password = Console.ReadLine();
            }

            AuthenticationUtility.SaveCredentials(serverUri, userName, password);
        }
    }
}
